﻿/* Preloader Class */
/* Developed by Carlos Yanez */

package 
{
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.SharedObject;
	import flash.display.Loader;
	import flash.net.URLRequest;
	
	public class Preloader extends MovieClip
	{
		/* Variable to store dataTextField param in the constructor function */
		
		private var dataTextField:TextField;
		
		public var ClickrLocalData:SharedObject = SharedObject.getLocal("ClickrSaveData");
		
		private var mouseWheelEvent:Loader;   
		
		private var loadingBar;
		private var loadingBarTXT;
		
		public function Preloader(){
			stop();
			
			Loading.stop();
			loadingBar = Loading.getChildByName("bar");
			loadingBarTXT = loadingBar.getChildByName("_TXT");
			addEventListener(NavigationEvent.LOAD_COMPLETE, onLoadComplete, true);
			
			//Start preloader
			this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
			this.loaderInfo.addEventListener(Event.COMPLETE, onComplete);
			
			
			//Block Wheel 
			mouseWheelEvent = new Loader();   
			mouseWheelEvent.load(new URLRequest("BlockWheelAS2.swf"));
			this.stage.addChild(mouseWheelEvent);
			
			
			//Load SharedObject
			if (ClickrLocalData.data.bestScore != undefined){	//저장된 파일이 있으면 로드
				// The SharedObject exists so display the data
				Clickr.bestScore = Number(ClickrLocalData.data.bestScore);
				Clickr.cubeArray = ClickrLocalData.data.cubeArray;
				Clickr.lastNickName = ClickrLocalData.data.lastNickName;
				
				trace("bestScore", ClickrLocalData.data.bestScore);
				trace("cubeArray", ClickrLocalData.data.cubeArray.join("* "));
				trace("bestScore", ClickrLocalData.data.lastNickName);
				
			} else {	//저장된 파일이 없으면 초기화
			
				// There is no SharedObject so display a warning
				//trace("There is no Shared Object saved");
				Clickr.bestScore = 0;
				Clickr.cubeArray = [0, 0, 0, 0, 0];
				Clickr.lastNickName = "";
				
				ClickrLocalData.data.bestScore = 0;
				ClickrLocalData.data.cubeArray = [0, 0, 0, 0, 0];
				ClickrLocalData.data.lastNickName = "";
			}
			
			
			ClickrLocalData.flush();	//기록을 로컬 데이터에 저장한다.
			
			stage.quality = "LOW";	//그래픽 퀄리티를 최저로 설정. (퍼포먼스 향상)
			
			

		}
		
		/* Set the percent loaded to the textfield */

		private function onProgress(event:ProgressEvent):void
		{
			
			var pcent:Number=event.bytesLoaded/event.bytesTotal*100;
			
			trace("loading", pcent);
			
			
			loadingBar.gotoAndStop(Math.floor(pcent / 10) + 1);
			
			loadingBarTXT.text=int(pcent);
			
		}

		
		//로딩이 끝났을 때
		private function onComplete(e:Event):void
		{
			/* Remove listeners */
			trace("Preloader complete");
			
			this.loaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
			this.loaderInfo.removeEventListener(Event.COMPLETE, onComplete);

			//프리로더가 사라지는 애니메이션을 재생
			Loading.play();
			
			
		}
		
		//로더 애니메이션 재생이 끝나면
		private function onLoadComplete(e:NavigationEvent):void{
			trace("LoadComplete event received. Go to Main Game");
			removeEventListener(NavigationEvent.LOAD_COMPLETE, onLoadComplete);
			this.gotoAndStop(3);
			//3프레임으로 이동. 
			
		}
		
	}
}